package leetcode_core.leetcode_5;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年01月10日 14:19
 */
public class IsValid {
    public boolean isValid(String s) {
        //标记
        Map<Character,Boolean> map = new HashMap<>();
        Stack<Character> stack = new Stack<>();
        map.put('(',true);
        map.put('{',true);
        map.put('[',true);
        int length = s.length();
        for(int i = 0;i<length;i++){
            char c = s.charAt(i);
            Boolean b = map.get(c);
            if(b !=null && b){
                stack.push(c);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                if(c == ')' && stack.peek() != '('){
                    return false;
                }else if(c == '}' && stack.peek()!= '{'){
                    return false;
                }else if(c == ']' && stack.peek()!='['){
                    return false;
                }else{
                    stack.pop();
                }
            }
        }
        return stack.isEmpty();
    }
    @Test
    public void test(){
        System.out.println(isValid("()"));
        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("(]"));
        System.out.println(isValid("["));
    }
}
